<?php
/* SVN FILE: $Id: start.php 36 2009-08-07 07:43:53Z phamshantuyet@gmail.com $ */
/**
*
* @author $Author: phamshantuyet@gmail.com $
* @version $Revision: 36 $
* @lastrevision $Date: 2009-08-07 07:43:53 +0000 (Fri, 07 Aug 2009) $
* @modifiedby $LastChangedBy: phamshantuyet@gmail.com $
* @lastmodified $LastChangedDate: 2009-08-07 07:43:53 +0000 (Fri, 07 Aug 2009) $
* @filesource $URL: http://ladybirdphp.googlecode.com/svn/trunk/website/fw24h/start.php $
*/

require APP_DIR.'/config/core.php';

require APP_DIR.'/app_startup.php';

$FW24H_ACTIVE_DB = 0;

require FW24H_ROOT.'/fw24h/fw24h_functions.php';

if( $_FW24H_ERROR_HANDLER == '') {
	$_FW24H_ERROR_HANDLER = 'fw24h_myErrorHandler';
}

set_error_handler( $_FW24H_ERROR_HANDLER);


/**
 * MainController
 * 
 * @package   
 * @author 
 * @copyright PhamShanTuyet
 * @version 2008
 * @access public
 */
class MainController {
	/**
	 * MainController::dispatch()
	 * 
	 * @param mixed $_APP_PATH_INFO_
	 * @return
	 */
	function dispatch( $_APP_PATH_INFO_) {
		
		// lay thong tin cau hinh
		global $_FW24H_CONFIG_; 
		
		array_shift( $_APP_PATH_INFO_);
		if( $_APP_PATH_INFO_[0] == 'admin') {
			array_shift( $_APP_PATH_INFO_);
			$_APP_PATH_INFO_[1] = 'admin_'.$_APP_PATH_INFO_[1];
		}
		$module = !empty($_APP_PATH_INFO_[0]) ? $_APP_PATH_INFO_[0] : "home";
        $action = !empty($_APP_PATH_INFO_[1]) ? $_APP_PATH_INFO_[1] : "index";
		
		// kiem tra ton tai cua controller
		$file = strtolower( APP_DIR.'/controllers/'.$module.'_controller.php');
		if (!file_exists( $file)) {
			include APP_DIR.'/views/layouts/missing.php';
            exit();
        }
		
		require FW24H_ROOT.'/fw24h/fw24h_controller.php';
		require APP_DIR.'/fw24h_app_controller.php';
		
		require_once $file;
		$class = ucfirst( $module).'Controller';
		$controller = new $class();
		
		// kiem tra ton tai cua action
		if (!method_exists($controller, $action)) {
			include APP_DIR.'/views/layouts/missing.php';
            exit();
        }
		
		require FW24H_ROOT.'/fw24h/driver/'.$_FW24H_CONFIG_['driver'].'.php';
		require FW24H_ROOT.'/fw24h/fw24h_model.php';
				
		// ket noi CSDL
		$GLOBALS['FW24H_ACTIVE_DB'] = fw24h_db_connect( $_FW24H_CONFIG_);

		$controller->actionName = $action;
		$controller->controllerName = $module;
		
		// load cac model dc khai bao
		$this->__loadModels( $controller);

		// load cac comp dc khai bao
		$this->__loadComponents( $controller);
		
		$params = array();
		if( count( $_APP_PATH_INFO_) > 2) {
			$params = $_APP_PATH_INFO_;
			array_shift( $params);
			array_shift( $params);
			$controller->_path_info = $params;
		}
		
		$this->__beforeFilter( $controller);
		
		$controller->__loadHelpers();
		
		call_user_func_array( array( &$controller, $action),  $params);
		
		$this->__afterFilter( $controller);

		if( $controller->blockNoCache) {
			$cache_file = APP_DIR.'/cache/'.$controller->layout;

			if( !( $v_data = fw24h_file_read_cache( $cache_file, $controller->block_cache_timeout))) {
				ob_start();
				ob_implicit_flush( false);

				$controller->displayView();

				$v_data = ob_get_contents();
				ob_end_clean();	
				fw24h_file_write_cache( $cache_file, $v_data);
			}else{
				while( list( $key, $value) = each( $controller->blockNoCacheArray))
				{
					if( eregi('<!-- BEGIN_NOCACHE: '.$key.' -->(.*)<!-- END_NOCACHE: '.$key.' -->', $v_data, $result)){
						$v_data = str_replace( $result[0], $value, $v_data);
					}
				}
			}
			echo $v_data;
		}else{
			$controller->displayView();
		}
	}

	function __loadModels( &$controller) {
		foreach( $controller->useModel as $v_key=>$v_value) {
			$v_model = ucfirst( $v_value);
			fw24h_useModel( $v_value);
			$v_model2 = $v_model.'Model';
			$controller->$v_model = new $v_model2();
		}
	}

	function __loadComponents( &$controller) {
		foreach( $controller->useComponent as $v_key=>$v_value) {
			$v_component = ucfirst( $v_value);
			fw24h_useComponent( $v_value);
			$v_component2 = $v_component.'Component';
			$v_component2 = new $v_component2();
			if( method_exists( $v_component2, 'startup')) {
				$v_component2->startup( $controller); // khoi dong comp xu ly controller
			}
			$controller->$v_component = $v_component2;
		}
	}
	
	function __beforeFilter( &$controller) {
		// goi ham tien xu ly
		$controller->beforeFilter();

		// goi cac ham tien xu ly dc khai bao trong danh sach.
		foreach( $controller->beforeFilter as $v_actionName=>$v_beforeFilter)
		{
			if( $v_actionName == $controller->actionName) {
				foreach( $v_beforeFilter as $v_method) {
					$controller->$v_method();
				}
				break;
			}
		}
	}

	function __afterFilter( &$controller) {
		foreach( $controller->afterFilter as $v_actionName=>$v_afterFilter) 
		{
			if( $v_actionName == $action) {
				foreach( $v_afterFilter as $v_method) {
					$controller->$v_method();
				}
				break;
			}
		}

		$controller->afterFilter();
	}

}

#

